package cn.itcast.web.controller.cargo;

import cn.itcast.domain.shipping.Shipping;
import cn.itcast.domain.shipping.ShippingExample;
import cn.itcast.service.cargo.ShippingService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/cargo/shipping")
public class ShippingController extends BaseController {

    @Reference(check = false,retries = 0)
    private ShippingService shippingService;

    //查看委托单列表
    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "5") Integer pageSize,String msg){
        if (msg!=null){
            String message = "已生成发票,无法重复生成";
            request.setAttribute("message",message);
        }
        ShippingExample shippingExample = new ShippingExample();

        //1. 查找到所有的委托单
        PageInfo<Shipping> pageInfo = shippingService.findByPage(shippingExample,pageNum,pageSize);
        //2. 把查询的委托单存储到request域中
        request.setAttribute("pageInfo",pageInfo);
        return "cargo/shipping/shipping-list";
    }

    //进入添加页面
    @RequestMapping("/toAdd")
    public String toAdd(String id){
        return "cargo/shipping/shipping-add";
    }

    //保存添加
    @RequestMapping("/edit")
    public String edit(Shipping shipping,String id){
        //String id = (String) request.getAttribute("packingListId");
        if (id!=null) {
            shipping.setShippingOrderId(id);
            shipping.setState(0);
            shippingService.save(shipping);
            return "redirect:/cargo/shipping/list.do";
        }else {
            return "redirect:/cargo/shipping/list.do";
        }
    }

    //删除
    @RequestMapping("/delete")
    @ResponseBody
    public String delete(String id){
        //1.根据委托单id删除
        Integer row = shippingService.delete(id);
        if (row==0){
            return "0";
        }else {
            return "1";
        }
    }

    //进入生成发票页面
    @RequestMapping("/toCreateInvoice")
    public String toCreateInvoice(String id){

        Shipping shipping = shippingService.findById(id);
        if (shipping.getState()==1) {
            String msg = "true";
            return "redirect:/cargo/shipping/list.do?msg="+msg;
        }
        request.setAttribute("shipping",shipping);

        return "cargo/invoice/invoice-add";
    }

}
